﻿Imports System.Data.SqlClient
Imports DevExpress.Web

Public Class AddMenu
    Inherits System.Web.UI.Page
    Dim dm As New DanhMuc_BLL()
    Dim p As New DanhMuc_DTO()



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        load_dm()

        If Page.IsPostBack Then
            Try
                load_pltheodm()
                load_dm()
            Catch
            End Try

        End If

    End Sub


    Public Sub load_dm()
        gridDM.DataSource = dm.DanhMuc()
        gridDM.DataBind()
        cb_danhmuc.DataSource = dm.DanhMuc()
        cb_danhmuc.DataBind()

    End Sub
    Public Sub load_pltheodm()
        p._tendm = cb_danhmuc.SelectedItem.Text
        gridpl.DataSource = dm.SoSach_PLTheoDm(p)
        gridpl.DataBind()
    End Sub


    Protected Sub cb_danhmuc_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cb_danhmuc.SelectedIndexChanged
        Try
            gridpl.Visible = True
            load_pltheodm()
            btn_them.Disabled = False
        Catch
            Dim scriptText As String
            scriptText = "return confirm('Do you want to submit the page?')"
            ClientScript.RegisterOnSubmitStatement(Me.GetType(), "ConfirmSubmit", scriptText)
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), True)
        End Try


    End Sub



    Protected Sub btn_hoantacdm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_hoantacdm.Click
        txt_danhmuc.Text = ""
    End Sub

    Protected Sub btn_xacnhanpl_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_xacnhanpl.Click
        Try
            p._tendm = cb_danhmuc.SelectedItem.Text
            p._phanloai = txt_phanloai.Text
            dm.Them_SubDanhMuc(p)
            txt_phanloai.Text = ""
            load_pltheodm()
            successDivPL.Attributes("class") = "alert alert-success"
            successDivPL.Visible = True
            lb_messpl.Text = "Thêm Thành Công!"

        Catch ex As SqlException
            txt_phanloai.Text = ""
            successDivPL.Attributes("class") = "alert alert-warning"
            successDivPL.Visible = True
            lb_messpl.Text = If(String.IsNullOrEmpty(ex.Message), "Thêm Thất Bại", ex.Message)
        End Try
    End Sub

    Protected Sub btn_hoantacpl_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_hoantacpl.Click
        txt_phanloai.Text = ""
    End Sub

    Protected Sub btn_xacnhandm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_xacnhandm.Click
        Try
            p._tendm = txt_danhmuc.Text
            dm.Them_DanhMuc(p)
            txt_danhmuc.Text = ""
            load_dm()
            successDivDM.Attributes("class") = "alert alert-success"
            successDivDM.Visible = True
            lb_messDM.Text = "Thêm Thành Công!"

        Catch ex As SqlException
            txt_danhmuc.Text = ""
            successDivDM.Attributes("class") = "alert alert-warning"
            successDivDM.Visible = True
            lb_messDM.Text = If(String.IsNullOrEmpty(ex.Message), "Thêm Thất Bại", ex.Message)

        End Try
    End Sub

    Protected Sub gridpl_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles gridpl.RowUpdating
        Try

            p._id = e.Keys("Id")
            p._phanloai = e.NewValues("phanloai")
            p._phanloaicu = e.OldValues("phanloai")
            dm.CapNhap_PL(p)
            gridpl.CancelEdit()
            load_pltheodm()
            e.Cancel = True
        Catch ex As SqlException
            gridpl.JSProperties("cp_showAlertMessage") = ex.Message
            e.Cancel = True
        End Try


    End Sub

    Protected Sub gridpl_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs) Handles gridpl.RowDeleting
        Try
            p._phanloai = e.Values("phanloai")
            dm.xoa_PL(p)
            load_pltheodm()
            e.Cancel = True

        Catch ex As SqlException
            gridpl.JSProperties("cp_showAlertMessage") = ex.Message
            e.Cancel = True
        End Try
    End Sub

    Protected Sub gridDM_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs) Handles gridDM.RowDeleting
        Try
            p._tendm = e.Values("tendm")
            dm.xoa_DM(p)
            e.Cancel = True
            load_dm()
        Catch ex As SqlException
            gridDM.JSProperties("cp_showAlertMessage") = ex.Message
            e.Cancel = True
        End Try

    End Sub

    Protected Sub gridDM_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles gridDM.RowUpdating
        Try
            p._id = e.Keys("id")
            p._tendm = e.NewValues("tendm")
            p._tendmcu = e.OldValues("tendm")
            dm.CapNhap_DM(p)
            load_dm()
            gridDM.CancelEdit()
            e.Cancel = True
        Catch ex As SqlException
            gridDM.JSProperties("cp_showAlertMessage") = ex.Message
            e.Cancel = True
        End Try
    End Sub
End Class